<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * VictoryCMS - VcmsSettings.php
 * 
 * @license http://www.gnu.org/licenses/gpl.html
 * @author Lewis Gunsch
 * @filesource
 * @package VcmsCore
 */

/**
 * This class contains the necessary web site specific
 * variables used by VictoryCMS.
 *
 * @package VcmsCore
 * 
 * @todo Needs a setup/update script to create the Settings file
 * @todo Create custom exception class to use here
 */
class VcmsSettings {

	/** The settings file path */
	//private static $settings_path;
	
	/** The current instance of the VcmsSettings class. */
	private static $instance;

	/** The path from the web root relative to the domain name. */
	private $web_prefix;

	/** The database type (MySql, Postgres, etc.) */
	private $db_type;

	/** The database server address to be used by VictoryCMS */
	private $db_hostname;

	/** The database username to be used by VictoryCMS */
	private $db_username;

	/** The database password to be used by VictoryCMS */
	private $db_password;

	/**
	 * The database to use
	 */
	private $db_name;

	/** The VictoryCMS database tables prefix */
	private $db_prefix;

	/** Maximum file size to be uploaded to the database */
	private $max_file_upload;
	
	public function __construct() {
		$this->web_prefix = "";
		$this->db_type = "";
		$this->db_hostname = "";
		$this->db_username = "";
		$this->db_password = "";
		$this->db_name = "";
		$this->db_prefix = "";
		$this->max_file_upload = "";

		$data = $this->loadSettingsFile();
		$dataStruct = $this->scanSettings( $data );
		
		return;
	}

	public function __destruct() {
		unset($this->web_prefix);
		unset($this->db_type);
		unset($this->db_hostname);
		unset($this->db_username);
		unset($this->db_password);
		unset($this->db_name);
		unset($this->db_prefix);
		unset($this->max_file_upload);
		return;
	}

	/**
	 * Returns an instance of the VcmsSettings class.
	 * 
	 * @return VcmsSettings An instance of the settings class
	 */
	public static function getInstance(){
		
		if(!isset(self::$instance)) {
			self::$instance= new VcmsSettings();
		}
		return self::$instance;
	}

	/**
	 * Scans, and parses the settings struct and sets the
	 * necessary VictoryCMS user settings.
	 */
	public function scanSettings( $data ) {

		$tag = "";
		$value = "";
		$prefix = "";
		$startParsing = false;
		foreach((array)$data as $subdata) {
			$tag  = $subdata["tag"];
			// Start parsing when vcmssettings open tag is seen
			if ($tag == "VCMSSETTINGS") {
				if ($subdata["type"] == "open") {
					$startParsing = true;
				} else {
					$startParsing = false;
				}
			} else if ($startParsing) {
				if (!empty($subdata['value'])) {
					$value = $subdata['value'];
				} else {
					$value = "";
				}
				
				switch ($tag) {
					case "WEB_PREFIX": $this->web_prefix = $value; break;
					case "DB_TYPE": $this->db_type = strtolower($value); break;
					case "DB_HOSTNAME": $this->db_hostname = $value; break;
					case "DB_USERNAME": $this->db_username = $value; break;
					case "DB_PASSWORD": $this->db_password = $value; break;
					case "DB_NAME": $this->db_name = $value; break;
					case "DB_PREFIX": $this->db_prefix = $value; break;
					case "MAX_FILE_UPLOAD": $this->max_file_upload = $value; break;
				}
			}
		}
		unset($subdata);
		return;
	}

	/**
	 * Loads the settings file for the setup of VictoryCMS into a struct.
	 *
	 * @return Struct The settings file in an struct
	 */
	private function loadSettingsFile() {

		// Load the settings file
		$settings_file = file(VarMap::get("settings_path"));
		
		if ($settings_file == FALSE) {
			throw new ParseSettingsException('Could not open file: '.$settings_file.', please check path.');
		}

		// Put settings file into a string
		$data = implode("", $settings_file);
		$parser = xml_parser_create();
		if ($parser != TRUE) {
			throw new ParseSettingsException('Could not create XML parser.');
		}

		// Set the xml parser options
		$errors = FALSE;
		$errors = xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
		if ($errors == FALSE) {
			throw new ParseSettingsException('Could not set the XML white skip option.');
		}
		$errors = xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 1);
		if ($errors == FALSE) {
			throw new ParseSettingsException('Could not set the XML case folding option.');
		}

		// Parse the data into a struct and send to parseSettingsStruct
		if (xml_parse_into_struct($parser, $data, $settings) == 0) {
			throw new ParseSettingsException('Could not parse file into structure.');
		} else {
			$this->scanSettings($settings);
		}

		xml_parser_free($parser);

		return $data;
	}

	/**
	 * Returns the web site path portion of the URL
	 * prepended to any pages of this web site.
	 * Example: www.somesite.com/some/folder/<other web site root here>/
	 * returns "/some/folder/"
	 *
	 * @return String The web site path prefix.
	 */
	public function getWebPrefix() {
		
		return $this->web_prefix;
	}

	/**
	 * Returns the VictoryCMS database type.
	 *
	 * @return the database type string
	 */
	public function getDbType() {

		return $this->db_type;
	}

	/**
	 * Returns the VictoryCMS database hostname.
	 *
	 * @return The VictoryCMS database hostname.
	 */
	public function getDbHostname() {

		return $this->db_hostname;
	}

	/**
	 * Returns the VictoryCMS database username.
	 *
	 * @return The VictoryCMS database username.
	 */
	public function getDbUsername() {

		return $this->db_username;
	}

	/**
	 * Returns the VictoryCMS database password.
	 *
	 * @return The VictoryCMS database password.
	 */
	public function getDbPassword() {

		return $this->db_password;
	}

	/**
	 * Returns the VictoryCMS database name.
	 *
	 * @return The VictoryCMS database name.
	 */
	public function getDbName() {

		return $this->db_name;
	}

	/**
	 * Returns the VictoryCMS database table prefix.
	 *
	 * @return The VictoryCMS database table prefix.
	 */
	public function getDbPrefix() {

		return $this->db_prefix;
	}
	
	/**
	 * Returns the maximum file upload size.
	 * 
	 * @return Integer The maximum file upload size in bytes.
	 */
	public function getMaxFileUpload() {
		
		$fileSize = 0;
		$converted = 0;
		$converted = sscanf( $this->max_file_upload, "%d", $fileSize );
		if ($converted != 1) {
			throw new UploadFileSizeException;
		}
		return $fileSize;
	}
	
	// Prevent users to clone the instance
	public function __clone() {
		throw new SingletonCopyException;
	}
}

?>
